# -*- coding: utf-8 -*-
"""
@Version : 
@Author  : ChenMZ
@Time    : 2025/09/22
@File    : dev_base.py
@Desc    : 
@Modify  : 
"""
import wmi
from typing import List, Union
from device.dev_entity.cpu_entity import CpuEntity
from device.dev_entity.abc_entity import DevEntity


class WMIDeviceBase:
    def __init__(self):
        self._wmi_obj = wmi.WMI()
        self._list_: List[DevEntity] = []

    def to_list(self):
        return self._list_

    def __len__(self):
        return len(self._list_)

    def __getitem__(self, item: Union[int, slice]):
        if isinstance(item, int):
            return self._list_[item] if item < len(self._list_) else None
        else:
            try:
                return self._list_[item]
            except Exception as e:
                print(e)
                return None

    def __iter__(self):
        return iter(self._list_)

    def __repr__(self):
        return [dev.to_dict() for dev in self._list_]

    def __str__(self):
        return [dev.to_dict() for dev in self._list_]


class CpuBase(WMIDeviceBase):
    def __init__(self):
        super().__init__()
        self._list_: List[CpuEntity] = []
        for dev in self._wmi_obj.Win32_Processor():
            self._list_.append(CpuEntity(dev))


if __name__ == '__main__':
    cpu = CpuBase()
    print(cpu)
